﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ArticleWeb.Models;
using Microsoft.AspNet.Identity;

namespace ArticleWeb.Controllers
{
    [Authorize]
    public class ArticleFragmentsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: ArticleFragments
        public ActionResult Index(int page = 1)
        {
            ViewBag.Tags = db.ArticleTags.Select(x => new IDName<int> { ID = x.ID, Name = x.TagText }).ToDictionary(x => x.ID);
            ViewBag.Catagories = db.ArticleCategories.Select(x => new IDName<int> { ID = x.ID, Name = x.Name }).ToDictionary(x => x.ID);
            ViewBag.Users = db.Users.Select(x => new IDName<string> { ID = x.Id, Name = x.UserName }).ToDictionary(x => x.ID);
            var query = db.ArticleFragments.OrderByDescending(x => x.ID);
            ViewBag.Total = query.Count();
            return View(query.ToList(page));
        }

        // GET: ArticleFragments/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleFragment ArticleFragment = db.ArticleFragments.Find(id);
            if (ArticleFragment == null)
            {
                return HttpNotFound();
            }
            return View(ArticleFragment);
        }

        // GET: ArticleFragments/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: ArticleFragments/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,ArticleID,Content,Length,Referenced,CategoryID,UpdateTime,EditorID,TagID,AuditState")] ArticleFragment ArticleFragment)
        {
            if (ModelState.IsValid)
            {
                db.ArticleFragments.Add(ArticleFragment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ArticleFragment);
        }

        // GET: ArticleFragments/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleFragment ArticleFragment = db.ArticleFragments.Find(id);
            if (ArticleFragment == null)
            {
                return HttpNotFound();
            }
            ViewBag.Tags = db.ArticleTags.Select(x => new IDName<int> { ID = x.ID, Name = x.TagText });
            return View(ArticleFragment);
        }

        public ActionResult Audit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleFragment ArticleFragment = db.ArticleFragments.Find(id);
            if (ArticleFragment == null)
            {
                return HttpNotFound();
            }
            ArticleFragment.UpdateTime = DateTime.Now;
            ArticleFragment.AuditState = ArticleAuditState.已审核;
            db.Entry(ArticleFragment).State = EntityState.Modified;
            db.SaveChanges();
            return Redirect(Request.Headers["referer"]);
        }

        // POST: ArticleFragments/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit([Bind(Include = "ID,Content,TagId")] ArticleFragment ArticleFragment)
        {
            if (ModelState.IsValid)
            {
                var m = db.ArticleFragments.Find(ArticleFragment.ID);
                m.Content = ArticleFragment.Content;
                m.Length = ArticleFragment.Content.Length;
                m.UpdateTime = DateTime.Now;
                m.EditorID = Guid.Parse(User.Identity.GetUserId());
                db.Entry(m).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ArticleFragment);
        }

        // GET: ArticleFragments/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleFragment ArticleFragment = db.ArticleFragments.Find(id);
            if (ArticleFragment == null)
            {
                return HttpNotFound();
            }
            if (Request.IsAjaxRequest())
                return this.JsonToken();
            return View(ArticleFragment);
        }

        // POST: ArticleFragments/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ArticleFragment ArticleFragment = db.ArticleFragments.Find(id);
            db.ArticleFragments.Remove(ArticleFragment);
            db.SaveChanges();
            if (Request.IsAjaxRequest())
                return this.JsonOk();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
